package com.jd.mysql.mgr.controller.mgrmachine;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.jd.mysql.mgr.pojo.MgrMachine;
import com.jd.mysql.mgr.service.IMgrMachineService;
import com.jd.mysql.mgr.util.GlobalStaticVariables;
import com.jd.mysql.mgr.util.PageUtil;
import com.jd.mysql.mgr.util.ParamUtil;
import com.jd.mysql.mgr.util.StringUtil;

public class AddMgrMachineController extends SimpleFormController {
	
	private IMgrMachineService mgrMachineService;
	private static Integer start=0;
	private static Integer limit=GlobalStaticVariables.limit;

	public void setMgrMachineService(IMgrMachineService mgrMachineService) {
		this.mgrMachineService = mgrMachineService;
	}

	public AddMgrMachineController() {
		this.setCommandClass(MgrMachine.class);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		// TODO Auto-generated method stub
		MgrMachine mgrMachine = (MgrMachine)command;
		Integer machineId = ParamUtil.getIntParam(request, "id", 0) ;
		if(StringUtil.isNull(mgrMachine.getCpuInfo())){
			request.setAttribute("msg", "cpu information is null");
	        return new ModelAndView("error");
		}
		if(StringUtil.isNull(mgrMachine.getDiscInfo())){
			request.setAttribute("msg", "disc information is null");
	        return new ModelAndView("error");
		}
		if(StringUtil.isNull(mgrMachine.getIp())){
			request.setAttribute("msg", "ip is null");
	        return new ModelAndView("error");
		}else{
			if(mgrMachine.getId()==null && machineId==0){
				MgrMachine machinetest = mgrMachineService.getMgrMachineByIp(mgrMachine.getIp());
				if(machinetest!=null){
					request.setAttribute("msg", "The machine is exists");
			        return new ModelAndView("error");
				}
			}
		}
		if(StringUtil.isNull(mgrMachine.getMachineRoom())){
			request.setAttribute("msg", "machine room is null");
	        return new ModelAndView("error");
		}
		if(StringUtil.isNull(mgrMachine.getMachineBrand())){
			request.setAttribute("msg", "machine brand is null");
	        return new ModelAndView("error");
		}
		if(StringUtil.isNull(mgrMachine.getStorageStructure())){
			request.setAttribute("msg", "sotreage structure is null");
	        return new ModelAndView("error");
		}
//		System.out.println(mgrMachine.getMountDiscInfo());
		
		if(mgrMachine.getId()!=null || machineId!=0){
			MgrMachine oldmgrMachine  = mgrMachineService.findMgrMachineById(machineId);
			if(oldmgrMachine!=null){
				oldmgrMachine.setCpuInfo(mgrMachine.getCpuInfo());
				oldmgrMachine.setDiscInfo(mgrMachine.getDiscInfo());
				oldmgrMachine.setHostName(mgrMachine.getHostName());
				oldmgrMachine.setIp(mgrMachine.getIp());
				oldmgrMachine.setMachineBrand(mgrMachine.getMachineBrand());
				oldmgrMachine.setMachineLocation(mgrMachine.getMachineLocation());
				oldmgrMachine.setMachineRam(mgrMachine.getMachineRam());
				oldmgrMachine.setMachineRoom(mgrMachine.getMachineRoom());
				oldmgrMachine.setMountDiscInfo(mgrMachine.getMountDiscInfo());
				oldmgrMachine.setStorageStructure(mgrMachine.getStorageStructure());
				mgrMachineService.updateMgrMachine(oldmgrMachine);
			}
		}else{
			MgrMachine mm = mgrMachineService.getMgrMachineByIp(mgrMachine.getIp());
			if(mm!=null){
				request.setAttribute("msg", "This machine has been used,check it out");
		        return new ModelAndView("error");
			}
			Integer groupId=(Integer)request.getSession().getAttribute("group");
			mgrMachine.setGroupId(groupId);
			mgrMachine.setYn(true);
			mgrMachineService.addMgrMachine(mgrMachine);
		}
		String index = request.getParameter("index");
		Map<String,Object> map = new HashMap<String,Object>();
		Integer start = PageUtil.setPageParam(map,index, limit);
		HttpSession session = request.getSession(true);
		Integer groupId = (Integer)session.getAttribute("group");
		Long totalCount = mgrMachineService.getTotalCount(groupId,true);
		List<MgrMachine> dtoList = mgrMachineService.getMgrMachineList(start, limit, true, null, groupId);
		map.put("totalCount", totalCount);
		map.put("dtoList",dtoList);
		return new ModelAndView("machine/machineList","map",map);
	}
	
}
